/**
 * 获取微信文章页面
 */
const articles = require('./articles.json');
const axios = require('axios');
const fs = require('fs');
const cheerio = require('cheerio');

async function main() {
    let count = 0;
    for (let i = 0; i < articles.length; i++) {
        let articleArr = [];
        if (i == articles.length - 1) {
            articleArr = articles[i].general_msg_list.list;
        } else {
            articleArr = JSON.parse(articles[i].general_msg_list).list;
        }
        for (let j = 0; j < articleArr.length; j++) {
            if (count > 15 || true) {
                await getArticle(articleArr[j].app_msg_ext_info.content_url);
                console.log(articleArr[j].app_msg_ext_info, 'is OK', ++count);
                let multi_app_msg_item_list = articleArr[j].app_msg_ext_info.multi_app_msg_item_list;
                if (multi_app_msg_item_list) {
                    for (let k = 0; k < multi_app_msg_item_list.length; k++) {
                        console.log(multi_app_msg_item_list[k].content_url);
                        await getArticle(multi_app_msg_item_list[k].content_url);
                        console.log(multi_app_msg_item_list[k], 'is OK', ++count);
                    }
                }
            } else {
                console.log(articleArr[j].app_msg_ext_info, 'isssss', ++count);
            }
        }
    }
}

async function getArticle(articleUrl) {
    // let a = await axios.get('https://mp.weixin.qq.com/s?__biz=MzI1MDIyNTcxMg==&mid=2247484389&idx=1&sn=8564c45eda911b3d85353910fe9e34ff&chksm=e98436badef3bfacecc3f9e39ce5930d29ca839e3455e9fb93a5df3cc968eeaea31215637596&mpshare=1&scene=1&srcid=05158iHsomBdYt403L0eCXCX#rd');
    console.log('articleUrl', articleUrl)
    let a = await axios.get(articleUrl);
    let data = a.data;
    let $ = cheerio.load(data);
    let title = $('#activity-name').text().replace(/[\s—|]/g,'');
    let timeMatch = data.match(/var publish_time = "(.*?)\"/)
    let time = '0000-00-00';
    if (timeMatch) time = timeMatch[1] || time;
    let imgs = $('img')
    let fileName = `${time} ${title}`;
    if (fs.existsSync(`${fileName}.html`)) {
        console.log(fileName, '已抓取 跳过')
        return;
    }
    for (let i = 0; i < imgs.length; i++) {
        let attribs = imgs[i].attribs;
        let url = attribs['data-src'];
        if (url) {
            let imgType = attribs.type || 'jpeg';
            if (!fs.existsSync(fileName)) {
                fs.mkdirSync(fileName)
            }
            let locPath = `./${fileName}/${i}.${imgType}`
            await getImg(url, locPath);
            imgs[i].attribs.src = locPath;
        }
    }
    fs.writeFileSync(`${fileName}.html`, $.html());
}

async function getImg(url, locPath) {
    let b =  await axios.get(url, {
        responseType: "arraybuffer",
    });
    fs.writeFileSync(locPath, Buffer.from(b.data, 'base64'));
}

// main()
getArticle('https://mp.weixin.qq.com/s/p4It1825oDJSBUZsSax1cA');
// getImg('https://mmbiz.qpic.cn/mmbiz_jpg/ySNHjEhfFgBXWGogwL4g4mELCc3MaSvfaPlm7mmXtYeiaXnTH6AfiaguYGYYAss3xEALteTpG1yJic3hA22GZkjEw/640?wx_fmt=jpeg');